package com.lx.txx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lx.bean.Result;
import com.lx.txx.dao.TxxCarMapper;
import com.lx.txx.entity.TxxCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 天下行租车-车辆表-10 前端控制器
 * </p>
 *
 * @author lx
 * @since 2024-12-30
 */
@RestController
@RequestMapping("/txxCar")
public class TxxCarController {

    @Autowired
    TxxCarMapper txxCarMapper;


    @RequestMapping("allCar")
    public Result allCar(){
        List<TxxCar> txxCars = txxCarMapper.selectList(null);
        return new Result(1, null, txxCars);
    }

    @RequestMapping("carInfo")
    public Result carInfo(String id){
        TxxCar txxCar = txxCarMapper.selectById(id);
        return new Result(1, null, txxCar);
    }

    @RequestMapping("imageByCid")
    public Result imageByCid(String id){
        List<TxxCar> txxCars = txxCarMapper.selectByCarId(id);
        return new Result(1, null, txxCars);
    }

    @RequestMapping("addCar")
    public Result addCar(@RequestBody TxxCar txxCar){
        // insert(txxCar) 会自动把主键映射到实体对象中
        txxCarMapper.insert(txxCar);
        return new Result(1, "添加成功", txxCar.getId());
    }

    @RequestMapping("searchBybrand")
    public List<TxxCar> searchBybrand(String brand){
        LambdaQueryWrapper<TxxCar> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TxxCar::getBrand, brand);
        return txxCarMapper.selectList(lqw);
    }

    @RequestMapping("searchByText")
    public List<TxxCar> searchByText(String searchText){
        LambdaQueryWrapper<TxxCar> lqw = new LambdaQueryWrapper<>();
        lqw.like(TxxCar::getBrand, searchText).or().like(TxxCar::getModel, searchText);
        return txxCarMapper.selectList(lqw);
    }

}
